/**
 * https://leetcode-cn.com/problems/daily-temperatures/
 * 每日温度 (求右边第一个比它大的数)
 * @param temperatures 
 */
const dailyTemperatures = (temperatures: number[]): number[] => {
    const len = temperatures.length;

    // 初始化数组, 每项都默认为0
    const arr = new Array(len).fill(0)

    const stack: number[] = []; // 模拟栈

    // 遍历
    for (let i = 0; i < len; i++) {
        // 栈中有元素并且栈顶小于当前元素
        while (stack.length > 0 && temperatures[i] > temperatures[stack[stack.length - 1]]) {
            // 弹出元素
            let top = stack.pop()!

            // 差值就是当前值 - 之前存入栈的索引
            arr[top] = i - top 
        }

        stack.push(i)
    }


    return arr;
};

console.log(dailyTemperatures([73,74,75,71,69,72,76,73]))
